package com.zyh.demo.reflect;

import com.zyh.demo.domain.Book;
import com.zyh.demo.utils.MyJson;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author zyh
 * @Date 2024/8/14 15:02
 * @Description 通过反射实现Java对象转JSON
 */

public class ReflectTest4 {
    @Test
    public void test1() {
        Book book = new Book("铁山靠", "cxk", 23.0, new Date());
        String s = MyJson.domainToJsonString(book);
        System.out.println("s = " + s);
    }

    @Test
    public void test2() {
        List<Book> list = new ArrayList<>();
        list.add(new Book("三国演义", "罗贯中", 23.0, new Date()));
        list.add(new Book("西游记", "吴承恩", 25.0, new Date()));
        list.add(new Book("水浒传", "施耐庵", 27.0, new Date()));
        list.add(new Book("红楼梦", "曹雪芹", 26.0, new Date()));
        String s = MyJson.multipleDomainToJsonString(list);
        System.out.println("s = " + s);
    }

    @Test
    public void test3() {
        List<Book> list = new ArrayList<>();
        list.add(new Book("三国演义", "罗贯中", 23.0, new Date()));
        list.add(new Book("西游记", "吴承恩", 25.0, new Date()));
        list.add(new Book("水浒传", "施耐庵", 27.0, new Date()));
        list.add(new Book("红楼梦", "曹雪芹", 26.0, new Date()));
        Book book = new Book("铁山靠", "cxk", 23.0, new Date());
        String s = MyJson.objectToJsonString(list);
        System.out.println("s = " + s);
        System.out.println("--------------");
        String s1 = MyJson.objectToJsonString(book);
        System.out.println("s1 = " + s1);
    }

    @Test
    public void test4() throws Exception {
        String str = "{\"bName\":\"坤家拳\",\"author\":\"cxk\",\"price\":23.0,\"createTime\":1723626435024}";
        Book book = MyJson.jsonStringToDomain(str);
        System.out.println("book = " + book);
    }

    @Test
    public void test5() {
        String str = "[{\"bName\":\"三国演义\",\"author\":\"罗贯中\",\"price\":23.0,\"createTime\":1723627131305},{\"bName\":\"西游记\",\"author\":\"吴承恩\",\"price\":25.0,\"createTime\":1723627131305},{\"bName\":\"红楼梦\",\"author\":\"曹雪芹\",\"price\":26.0,\"createTime\":1723627131305}]";
        List<Book> list = MyJson.jsonStringToList(str);
        for (Book book : list) {
            System.out.println("book = " + book);
        }
    }
}
